package com.hxm.canvas;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by hxm on 2017/7/13.
 * 描述：
 */
public class DrawTest1 extends View {

    Paint paint = new Paint();
    Paint paint1 = new Paint();
    Paint paint2 = new Paint();
    RectF rect = new RectF();
    RectF rectF = new RectF();
    private Context context;
    private int mScreenWidth = 0; // 屏幕宽
    private int mScreenHeight = 0; // 屏幕高
    private Bitmap bitmap;
    public DrawTest1(Context context) {
        this(context, null);
    }

    public DrawTest1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawTest1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
        Bitmap map = BitmapFactory.decodeResource(context.getResources(), R.drawable.paper);
        bitmap = Bitmap.createBitmap(map, 0, 0, mScreenWidth, mScreenHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(ContextCompat.getColor(context, R.color.lightGray));
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(ContextCompat.getColor(context, R.color.black));
        paint.setStrokeWidth(SizeUtil.dp2px(context, 1));
        paint.setTextSize(SizeUtil.dp2px(context, 14));
        paint.setAntiAlias(true);
//        canvas.drawCircle(200, 200, 100, paint);
//
//        paint1.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
//        paint1.setStrokeWidth(SizeUtil.dp2px(context, 30));
//        paint1.setStrokeCap(Paint.Cap.ROUND);
//        canvas.drawPoint(100, 100, paint1);
//        float[] points = {0, 0, 40, 50, 89, 30, 400, 500, 322, 455, 133, 90, 344, 600};
//        canvas.drawPoints(points, paint1);
//        canvas.drawLine(200, 200, 400, 400, paint1);
//
        paint1.setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeWidth(SizeUtil.dp2px(context, 1));
        paint1.setTextSize(SizeUtil.dp2px(context, 14));
        paint1.setAntiAlias(true);
//        rect.set(100, 300, 700, 500);
//        canvas.drawOval(rect, paint1);
//
//        rect.set(400, 300, 700, 900);
//        canvas.drawArc(rect, 0, 70, false, paint1);

//        paint2.setColor(ContextCompat.getColor(context, R.color.colorAccent));
//        paint2.setStyle(Paint.Style.FILL);
//        paint2.setStrokeWidth(SizeUtil.dp2px(context, 1));
//        rect.set(200, 200, 400, 400);
//        canvas.drawRect(rect, paint2);
        canvas.drawBitmap(bitmap, 0, 0, null);
        canvas.save();
        canvas.translate(400, 400);
        canvas.skew(0.732f, 0);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint);
        canvas.rotate(20);
        canvas.drawText("Android开发", 0, 0, paint1);
        canvas.rotate(20);
        canvas.restore();
//        paint.setStyle(Paint.Style.STROKE); // 画线模式
        rectF.set(300, 600, 800, 900);
        canvas.drawArc(rectF, 180, 60, true, paint); // 绘制不封口的弧形
        Path path2 = new Path();
        path2.moveTo(100, 100);//设置Path的起点
/**
 * 参数1、2：x1，y1为控制点的坐标值
 * 参数3、4：x2，y2为终点的坐标值
 */
        path2.quadTo(300, 100, 400, 400); //设置贝塞尔曲线的控制点坐标和终点坐标
        path2.quadTo(500, 700, 800, 800);
        canvas.drawPath(path2, paint1);//画出贝塞尔曲线
        canvas.drawTextOnPath("abcdefghigklmnopqrstuvwxyz设置贝塞尔曲线的控制点坐标和终点坐标", path2, 0, 0, paint1);
    }
}
